<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/6
 * Time: 14:11
 */

/*
 *
 *  #测试脚本代码
    $runtime= new runtime;
    $runtime->start();
    $a = 0;
    for($i=0; $i<100000; $i++)
    {
        $a *= $i;
    }
    $runtime->stop();
    $spent_time = $runtime->spent($echo=true, '测试脚本');
    $runtime->clear();
 */

namespace App\Libs;


class Runtime
{
    public $StartTime = 0;
    public $StopTime = 0;

    public function get_microtime()
    {
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
    }

    public function start()
    {
        $this->StartTime = $this->get_microtime();
    }

    public function stop()
    {
        $this->StopTime = $this->get_microtime();
    }

    public function spent($echo = false, $title = '')
    {
        $spent = sprintf('%.4f', round(($this->StopTime - $this->StartTime) * 1000, 1));
        //毫秒
        if ($echo) {
            echo $title . "执行时间:{$spent}毫秒" . PHP_EOL;
        } else {
            return $spent;
        }
    }

    public function clear()
    {
        $this->StartTime = 0;
        $this->StopTime = 0;
    }

}